package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.dao.CheckGroupDao;
import com.xrit.sunnyday.dao.CheckItemDao;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.CheckGroup;
import com.xrit.sunnyday.pojo.CheckItem;
import com.xrit.sunnyday.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
     * 查询所有检查组（不分页）
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    /**
     * 查询所有检查组（分页）
     * @param queryPageBean
     * @return
     */
    @Override
    public PageInfo<CheckGroup> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        List<CheckGroup> list = checkGroupDao.findPage(queryPageBean.getQueryString());
        return new PageInfo<>(list);
    }

    /**
     * 添加检查组和拥有的检查项
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.addCheckGroup(checkGroup);

        HashMap<String,Object> map = new HashMap<>();
        map.put("gid",checkGroup.getId());
        map.put("iids",checkitemIds);
        checkGroupDao.addGroupItemId(map);
    }

    /**
     * 点击编辑根据ID查询检查组以及拥有的检查项
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }
    @Override
    public List<Integer> findcheckitemIds(Integer id) {
        return checkGroupDao.findcheckitemIds(id);
    }

    /**
     * 根据ID修改检查组以及拥有的检查项
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    public void updata(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.update(checkGroup);

        checkGroupDao.deletegroupitemById(checkGroup.getId());
        HashMap<String,Object> map = new HashMap<>();
        map.put("gid",checkGroup.getId());
        map.put("iids",checkitemIds);
        checkGroupDao.addGroupItemId(map);
    }

    /**
     * 根据ID查询是否被套餐关联，没有就删除与检查项的关系，在删除检查组
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
        int count = checkGroupDao.findsetmealgroupId(id);
        if(count>0){
            throw new SunnydayException("该检查组已经被套餐关联");
        }

        checkGroupDao.deletegroupitemById(id);
        checkGroupDao.deleteById(id);
    }


}
